<template>
  <div class="login">
    <div class="card">
      <p class="p1">在线教育网后台管理系统</p>
      <el-form :rules="rules" ref="form" label-width="100px" :model="form">
        <el-form-item label="用户名" prop="user">
          <el-input v-model="form.user"  placeholder="用户名" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd" >
          <el-input v-model="form.pwd" type="password" placeholder="密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="login">登录</el-button>
      <p class="p" @click="register">还没有账号？前往注册</p>
    </div>
    <button @click="zb">作弊按钮</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        user: "剑雨SNOW",
        pwd: "",
      },

      rules: {
        user: [
          { required: true, message: '请输入用户名或邮箱', trigger: 'blur' },
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      }
    };
  },

  methods: {
    login() {
      this.$refs.form.validate(async (valid) => {
        if (!valid)  return this.$message.error("校验失败")
          var res = await this.axios({
            url: '/api/users/login',
            method: 'POST',
            data: {
              email: this.form.user,
              password: this.form.pwd,
            }
          })
          var token = res.data.token
          localStorage.setItem('token', token)
          localStorage.setItem('user', this.form.user)
          if (res.data.success == true) {
            this.$router.push('/menu')
            this.$message.success(`欢迎${this.form.user}`)
          } else {
            this.$message(`不欢迎${this.form.user},不让进`)
          
        }
      })
    },
    register() {
      this.$router.push('/register')
    },
    zb() {
      localStorage.setItem('token', "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYyMmViMDdlZmY4M2YzNGQ1ZDYwMjE1NyIsIm5hbWUiOiJ3dWhhaXlhbmciLCJhdmF0YXIiOiIvL3d3dy5ncmF2YXRhci5jb20vYXZhdGFyLzFjYjRlMDRlZjlmN2QzNjhhM2QwYjljMzAxZTUwYzk4P3M9MjAwJnI9cGcmZD1tbSIsImlkZW50aXR5IjoibWFuYWdlciIsImlhdCI6MTY2MzU1Nzk1MiwiZXhwIjoxNjYzNTYxNTUyfQ.TLHhp5EDtXH-9oTSXRM3yL5nW5O_EYRfdqG9s5u76lc")
    }
  },
};
</script>
<style >
.login {
  width: 100vw;
  height: 100vh;
  background-color: rgb(45, 58, 75);
  position: relative;
}

.card {
  width: 600px;
  height: 300px;
  /* border: 1px solid #000; */
  /* line-height: 75px; */
  text-align: center;
  padding: 0 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.card .p1 {
  color: white;
  font-size: 25px;
  font-weight: 900;
}

.card .el-input__inner {
  background-color: rgb(40, 52, 67);
  ;
  border: none;
  color: white;
}

.card .el-button {
  width: 100%;

}

.card .p {
  width: 200px;
  height: 30px;
  color: rgb(11, 76, 149);
  font-size: 8px;
  line-height: 30px;
  margin:20px 400px;
}
.el-form-item__label{
  color: white;
}
</style>